package com.ccx.coachsite.downloadmgr;


import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ccx.coachsite.R;

public class DownloadMainActivity extends ActivityGroup {

	/*---------------------控件区------------------------------*/
	private CheckedTextView ctv_downloading;
	private CheckedTextView ctv_downloaded;
	private LinearLayout ll_container;
	private RelativeLayout ll_banner;
	private Button back_btn;
	
	/*----------------其他变量-----------------------------*/
	private final String ACTIVITY_KEY_DOWNLOADING="activity_key_downloading"; //正在下载
	private final String ACTIVITY_KEY_DOWNLOADED="activity_key_soft_downloaded";//已下载
	
	/*----------------公共变量-----------------------------*/
	private Context mContext;
	private CheckedTextView mDefaultSelectedView;//默认选中的标签卡
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_mgr_main);
		mContext=this;
		initView();
	}
	
	private void initView()
	{
		ctv_downloading=(CheckedTextView) findViewById(R.id.ctv_downloading);
		ctv_downloaded=(CheckedTextView) findViewById(R.id.ctv_downloaded);
		ll_container=(LinearLayout) findViewById(R.id.ll_container);
		ll_banner=(RelativeLayout)findViewById(R.id.ll_banner);
		back_btn = (Button) findViewById(R.id.back_btn);
		
		  boolean b=true;
		  if(b)
			  ll_banner.setVisibility(View.GONE);
		
		
		//初始默认显示的页面：正在下载或者已下载界面
		if(DownloadService.hasDownloadingTask())
		{
			//有正在下载的任务，则默认显示的是正在下载的页面
			mDefaultSelectedView=ctv_downloading;
			changeMainView(DownloadingActivity.class,ACTIVITY_KEY_DOWNLOADING);
		}else{
			//显示已下载界面
			mDefaultSelectedView=ctv_downloaded;
			changeMainView(DownloadedActivity.class,ACTIVITY_KEY_DOWNLOADED);
		}
		mDefaultSelectedView.setChecked(true);	
		NavMenuClickListener navMenuClick=new NavMenuClickListener();
		ctv_downloading.setOnClickListener(navMenuClick);
		ctv_downloaded.setOnClickListener(navMenuClick);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DownloadMainActivity.this.finish();
			}
		});
	}
	
	private void changeMainView(Class targetClass,String activityKey)
    {
    	Intent reqSoftIntent=new Intent(mContext,targetClass);
    	LocalActivityManager laMgr=getLocalActivityManager();
    	View mainView=null;
    	mainView=laMgr.startActivity(activityKey, reqSoftIntent).getDecorView();
    		
    	LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    	mainView.setLayoutParams(param);
    	ll_container.removeAllViews();
    	ll_container.addView(mainView);
    }
	
	
	/*---------------------内部类区------------------------------*/
	/**
     * 监听类
     * 头部菜单的点击事件监听
     */
    private class NavMenuClickListener implements OnClickListener{

    	private CheckedTextView mOldSelectedView=mDefaultSelectedView; //默认选中的标签卡
    	@Override
    	public void onClick(View v) {
    		
    		//点中的菜单与之前选中菜单不是同一个
    		if(mOldSelectedView!=null&&mOldSelectedView.getId()!=v.getId())
    		{
    			mOldSelectedView.setChecked(false);
    			//设置点中的菜单为选中的
        		((CheckedTextView)v).setChecked(true);
    			//记录选中的菜单，供下次点击菜单时进行状态切换
        		mOldSelectedView=((CheckedTextView)v);
        		
        		/*-------------跳转Activity--------------*/
        		if ( v.getId()==R.id.ctv_downloading ) {
        			changeMainView(DownloadingActivity.class,ACTIVITY_KEY_DOWNLOADING);
        		}
        		
        		if ( v.getId()==R.id.ctv_downloaded ) {
        			changeMainView(DownloadedActivity.class,ACTIVITY_KEY_DOWNLOADED);
        		}         			
    		}
    		
    	}
    	
    } //end NavMenuClickListener
	

}
